﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Water.Web.Controls.Util
{
    public class Utils
    {
        public static Control FindControlEx(string ID, Control parent)
        {
            Control ctl = null;
            ctl = parent.FindControl(ID);
            if(null == ctl)
            {
                foreach (Control _cCtl in parent.Controls)
                {
                    ctl = FindControlEx(ID, _cCtl);
                    if(null != ctl)
                       return ctl;
                }
            }
            return ctl;
        }

        //public static Control FindControl(Control control, string controlID)
        //{
        //    Control namingContainer = control;
        //    Control control3 = null;
        //    if (control != control.Page)
        //    {
        //        while ((control3 == null) && (namingContainer != control.Page))
        //        {
        //            namingContainer = namingContainer.NamingContainer;
        //            if (namingContainer == null)
        //            {
        //                throw new HttpException(SR.GetString("DataBoundControlHelper_NoNamingContainer", new object[] { control.GetType().Name, control.ID }));
        //            }
        //            control3 = namingContainer.FindControl(controlID);
        //        }
        //        return control3;
        //    }
        //    return control.FindControl(controlID);
        //}


        public static string GeteEventId(Page page)
        {
            string eventId = string.Empty;
            if (page.IsPostBack)
            {
                if (!string.IsNullOrEmpty(page.Request.Params["__EVENTTARGET"]))
                    eventId = page.Request.Params["__EVENTTARGET"];
                else
                {
                    foreach (string key in page.Request.Form.AllKeys)
                    {
                        if (!key.StartsWith("__"))
                        {
                            if (key.EndsWith(".x") || key.EndsWith(".y"))
                            {
                                eventId = key.Substring(0, key.Length - 2);
                                break;
                            }

                            Control c = page.FindControl(key);
                            if (c is System.Web.UI.WebControls.Button || c is System.Web.UI.WebControls.ImageButton)
                            {
                                eventId = key;
                                break;
                            }
                        }
                    }
                    int ind=eventId.LastIndexOf("$");
                    if (ind != -1)
                        eventId = eventId.Substring(ind + 1);
                }
            }
            else
                eventId = "page_load";

            return eventId;
        }

    }
}
